//utils.js
import axios from "axios";

// 创建 axios 实例
const axiosInstance = axios.create({
  baseURL: "http://127.0.0.1",
  timeout: 5000,
});

// 请求拦截器
axiosInstance.interceptors.request.use(
  (config) => {
    // 确保请求体正确传递
    if (config.body) {
      config.data = config.body;
      delete config.body;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
axiosInstance.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    console.error('Request error:', error);
    return Promise.reject(error);
  }
);

export const request = (options) => {
  return axiosInstance(options);
};
